<template>
  <el-popover trigger="focus" :width="width" :disabled="list.length == 0">
    <!-- v-model="visible" -->
    <div class="auto-complete">
      <div v-for="(row, i) in list" :key="i" class="auto-complete-tr">
        <div v-if="row.name" class="auto-complete-td frist">
          {{ row.name }}
        </div>

        <div class="auto-complete-td">
          <div v-for="(child, j) in row.childList" :key="j" class="auto-complete-cell">
            <div v-if="child.name" class="auto-complete-cell two">{{ child.name }}</div>
            <div class="auto-complete-cell childList">
              <el-link
                v-for="(item, k) in child.childList"
                :key="k"
                :underline="false"
                class="auto-complete-cell linkItem"
                @click="selectItem(item.name)"
              >
                {{ item.name }}
              </el-link>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-input
      slot="reference"
      v-model="value"
      placeholder="请选择或输入"
      type="textarea"
      maxlength="100"
      show-word-limit
      class="contentInput"
      :class="border?'border':''"
      @change="inputChange"
    />
  </el-popover>
</template>
<script>

import { selectByQueryDetail } from '@/api/doctorTemplate';
export default {
  name: 'InputAutoComplete',
  props: {
    inputVal: {
      type: String,
      default: ''
    },
    id: {
      type: String,
      default: ''
    },
    tag: {
      type: String,
      default: ''
    },
    plate: {
      type: String,
      default: '3'// 3看诊，4理疗
    },
    border: {
      type: Boolean,
      default: false
    },
    width: {
      type: String,
      default: '800'
    }
  },
  data() {
    return {
      value: '',
      list: [], // 选项
      level: '',
      visible: true
    };
  },
  watch: {
    inputVal(newVal) {
      this.value = newVal + '';
    }
  },
  mounted() {
    this.getDetail();
    this.value = this.inputVal;
    this.inputChange();
    console.log(this.border);
  },
  methods: {
    async getDetail() {
      const res = await selectByQueryDetail({
        plate: this.plate,
        dictId: this.id
      });
      this.level = res.data.level;
      this.list = res.data.levelText;
    },
    selectItem(name) {
      if (this.value.length == 100 || (this.value.length + name.length) > 100) return;
      if (this.value) {
        this.value += '，';
      }
      this.value += name;
      this.inputChange();
    },
    inputChange() {
      this.$emit('setData', this.tag, this.value);
    }
  }
};
</script>

<style lang="scss">
@import "../../assets/css/mixin";
.contentInput {
  width: 100%;
  height: 100%;
  .el-textarea__inner {
    height: 100%;
    background: transparent;
    border: 0;
    // font-size: 12px;
    @include add-small-size($font_size_12);
    color: #606266;
  }
}
.border {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}
.auto-complete-tr {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebeef5;
  &:last-child {
    border-bottom: 0;
  }
  .auto-complete-td {
    display: flex;
    flex-direction: column;
    border-left: 1px solid #ebeef5;
    &:first-child {
      border-left: 0;
    }
    & > .auto-complete-cell {
      border-bottom: 1px solid #ebeef5;
      &:last-child {
        border-bottom: 0;
      }
    }
    .auto-complete-cell {
      display: flex;
      justify-content: flex-start;
      min-height: 30px;
      line-height: 30px;
      align-items: center;
      &.linkItem {
        padding: 0 10px;
        line-height: 30px;
        // font-size: 14px;
        @include add-centre-size($font_size_14);
      }
      &.childList {
        flex-wrap: wrap;
        border-left: 1px solid #ebeef5;
      }
      &.two {
        width: 100px !important;
        justify-content: center;
        flex-shrink: 0;
      }
    }
    &.frist {
      width: 100px !important;
      align-items: center;
      flex-shrink: 0;
    }
  }
}
</style>
